import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

import 'package:fluwx/fluwx.dart' as fluwx;
class SurplusMarketLogic extends GetxController {
  final SurplusMarketState state = SurplusMarketState();

  @override
  void onInit() {
    super.onInit();
    print('我进来拉............${Get.arguments}');
    state.shop_id = SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'].toString();
    this.getSurplusMarket();
  }

  void getSurplusMarket() {
    var params = {
      'use_platform': 2,
      'ec_shop_num_id': SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      // 活动id, 暂时不知道是哪里获取id,,
      'activities':
          Get.arguments != null ? [Get.arguments['widgets_data_id']] : [],
      'classify': 8, // 8超级团 9定金预售
    };
    // var params = {
    //   'use_platform': 3,
    //   'ec_shop_num_id': 102002001,
    //   'activities': [],
    //   'classify': 8,
    // };
    Api.surplusMarketInfo(params).listen((event) {
      print('$params----$event');
      if (event != null && event['code'] == 0) {
        state.catDetails = event['cat_details'] ?? []; // 分类列表
        state.catDetails.insert(0, {
          'cat1_id': 1666,
          'cat_name': '全部',
          'cat_ids': [],
        });
        state.configInfo = event['config_info']; // 配置
        state.goodsList = event['goods_list']; // 商品列表
        state.showGoodsList = event['goods_list'];
      }
      update();
    });
  }

  void changeTabId(int id) {
    state.tabId = id;
    state.showGoodsList = [];
    state.goodsList.asMap().forEach((key, value) {
      if (value['cat_id'] == id || id == 1666) {
        state.showGoodsList.add(value);
      }
    });
    update();
  }


  void shareToFriend() {
    var       data =      'pintuan/pages/superMarket';
    WeChatImage img=WeChatImage.asset('assets/images/defaultAvatar_image.png');
    var title='超级团';
    var desc='超级团';
    if(state.configInfo!=null ) {
      if (state.configInfo['share_images'] != null &&
          state.configInfo['share_images'].length > 0) {
        img =WeChatImage.network( state.configInfo['share_images'][0]);
      }
      if (state.configInfo['share_title']!=null && state.configInfo['share_title']!='') {
        title = state.configInfo['share_title'];
      }
      if (state.configInfo['share_desc']!=null && state.configInfo['share_desc']!='') {
        desc = state.configInfo['share_desc'];
      }
    }
        var model = new WeChatShareMiniProgramModel(
        path: data,
        webPageUrl: 'www.baidu.com',
        userName: Config.MINI_ACCOUNT,
        miniProgramType:
        Config.isDev ? WXMiniProgramType.TEST : WXMiniProgramType.RELEASE,
        title: title,
        description: desc,
        thumbnail: img,
          hdImagePath: img,
      );
      fluwx.shareToWeChat(model);

  }
}
